<?php
/**
 * Created by PhpStorm.
 * User: 17732
 * Date: 2018/6/20
 * Time: 0:20
 */

namespace app\admin\controller;


use app\admin\model\Horn;
use app\admin\model\User;
use think\Controller;
use think\Request;

class Scrollmember extends BaseController
{
    /**
     * 滚屏
     */
    public function scroll(){
        if (!$this->checkIndex()){
            $this->redirect('/index/index/login');
        }
        if(empty($_POST['txt'])){
            return view();
        }
        $txt = $_POST['txt'];
        $data['content'] = $txt;
        $data['instime'] = time();
        $data['starttime'] = $data['instime'];
        $data['expectStoptime'] = $data['instime'] + 604800;
        $result = Horn::addHorn($data);

        // 成功返回1 失败返回0
        if($result){
            return 1;
        }
        else{
            return 0;
        }
    }
    /**
     * 查询所有滚屏
     */
    public function allHorn(){
        $page = !empty($_POST['page'])?$_POST['page']: 1;
        $query = Horn::selectAllHorn();
        $query = Publicmethod::pageing($query,$page,9);
        if(empty($query)){
            return 0;
        }
        else{
            return $query;
        }
    }
    /**
     * 删除滚屏
     */
    public function deleteScroll(){
        $data['id'] = $_POST['id'];
        $data['isdelete'] = 1;
        $data['isScroll'] = 0;
        $data['stoptime'] = time();
        $data['deleteTime'] = $data['stoptime'];
//        $data['isScroll'] = 0;
        $result = Horn::editHorn($data);
        if($result){
            return 1;     // 更新成功返回 1
        }
        return 0;   // 更新失败返回 0
    }
    /**
     * 停止滚屏
     */
//    public function stopScroll(){
//        $data['id'] = $_POST['id'];
//        $data['isScroll'] = 0;
//        $data['stoptime'] = time();
//        $result = Horn::editHorn($data);
//        if($result){
//            return 1;     // 更新成功返回 1
//        }
//        return 0;   // 更新失败返回 0
//    }
    /**
     * 会员管理
     */
    public function member(Request $request){
        if (!$this->checkIndex()){
            $this->redirect('/index/index/login');
        }
        $arr = $request->post();
        if(empty($arr)){
            return view();
        }
        $data['name'] = $arr['username'];
        $data['account'] = $arr['useraccounts'];
        $data['pass'] = md5('yxk666666');
        $data['instime'] = time();
        // 先查询这个人的帐号是否注册了，如果未注册就进行注册，已注册返回 2
        $query = User::selectUser($data['account']);
        if(empty($query)){
            $addUser = User::addUser($data);
            if($addUser){
                return 1; // 注册成功返回 1
            }
            else{
                return 0; // 注册失败返回 0
            }
        }
        else{
            return 2;    // 已有人注册返回 2
        }
    }
    /**
     * 查询出所有人员
     * data.totalpage;
     * data.currentPage; 当前页
     */
    public function memberArr(){
        $page = !empty($_POST['page'])?$_POST['page']: 1;
        $memberArr = User::selectAllUser();
        $meberList = Publicmethod::pageing($memberArr,$page,9);
        if(empty($meberList)){
            return 0; // 为空返回 0
        }
        else{
            return $meberList;
        }
    }

    /**
     * 停用用户
     */
    public function stopUser(){
        $id = $_POST['id'];
        $result = User::updateUserOne($id,'ustates',2);
        if($result){
            return 1;     // 更新成功返回 1
        }
        return 0;   // 更新失败返回 0
    }
    /**
     * 启用用户
     */
    public function startUser(){
        $id = $_POST['id'];
        $result = User::updateUserOne($id,'ustates',1);
        if($result){
            return 1;     // 更新成功返回 1
        }
        return 0;   // 更新失败返回 0
    }
    /**
     * 重置密码
     */
    public function resetPass(){
        $id = $_POST['id'];
        $pass = md5('yxk666666');
        $result = User::updateUserOne($id,'pass',$pass);
        if($result){
            return 1;     // 更新成功返回 1
        }
        return 0;   // 更新失败返回 0
    }

}